<template>
    <div class="page-container">
        <div class="page-layout">
            <div class="page-layout-header">
                <div class="header-aside flex-start-title">
                    <div class="title">{{ $t('platform.user.titleSide') }}</div>
                </div>
                <div class="header-main">
                    <div class="title">{{ $t('platform.user.title') }}</div>
                </div>
            </div>
            <div class="page-layout-body">
                <div class="body-aside">
                    <!--平台列表-->
                    <component-page-org-tree hide-root init-first-node @click="treeSelectNode"></component-page-org-tree>
                </div>
                <div class="body-main flex flex-col" v-if="tree.id != null" v-loading="loading">
                    <div class="page-filter-option">
                        <el-form :inline="true" :model="filterOption" class="flex flex-wrap">
                            <el-form-item>
                                <el-input :placeholder="$t('platform.user.email')" prefix-icon="el-icon-search" clearable
                                          v-model="filterOption.keyword" @change="filterChange"></el-input>
                            </el-form-item>
                            <el-form-item class="small">
                                <el-select :placeholder="$t('platform.user.deleted')" v-model="filterOption.deleted" clearable @change="filterChange">
                                    <el-option v-for="(item, index) in $t('form.deletedList')" :key="index" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="filterChange">{{ $t('action.search') }}</el-button>
                            </el-form-item>
                            <el-form-item class="ml-auto mr-0">
                                <el-button type="primary" plain icon="el-icon-plus" @click="dialogItemCreate">{{ $t('action.new') }}</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div class="list-space"></div>
                    <div class="page-container-inner-fluid flex-auto flex flex-col">
                        <component-page-content-resize v-slot="{ content }">
                            <el-table style="width: 100%" :height="content.height"
                                      :data="dataList"
                                      :default-sort="{prop: filterOption.order_column, order: filterOption.order_key}"
                                      @sort-change="filterOrderChange"
                                      highlight-current-row>
                                <el-table-column min-width="80" label="ID" prop="id" sortable="custom" :sort-orders="['descending', 'ascending']"></el-table-column>
                                <el-table-column min-width="150" :label="$t('platform.user.name')" prop="name"></el-table-column>
                                <el-table-column min-width="200" :label="$t('platform.user.email')">
                                    <template slot-scope="scope">
                                        <span v-html="$options.filters.hsFilterKeyword(scope.row.email, filterOption.keyword)"></span>
                                    </template>
                                </el-table-column>
                                <el-table-column min-width="120" :label="$t('platform.user.roles')">
                                    <template slot-scope="scope">
                                        <span v-if="!!scope.row.is_super_admin">
                                            <el-tag effect="plain" size="mini" type="danger">Super-Admin</el-tag>
                                        </span>
                                        <span v-else>
                                            <el-tag effect="plain" size="mini" type="info">null</el-tag>
                                        </span>
                                    </template>
                                </el-table-column>
                                <el-table-column min-width="220" :label="$t('platform.user.updated')" prop="updated" sortable="custom" :sort-orders="['descending', 'ascending']">
                                    <template slot-scope="scope">
                                        <component-page-timestamp :timestamp="scope.row.updated"></component-page-timestamp>
                                    </template>
                                </el-table-column>
                                <el-table-column min-width="100" :label="$t('platform.user.deleted')" prop="deleted">
                                    <template slot-scope="scope">
                                        <el-tag effect="plain" size="mini" :type="!scope.row.deleted ? 'info' : 'danger'">
                                            {{ $t('form.deletedList')[Number(!!scope.row.deleted)].label }}
                                        </el-tag>
                                        <span class="operation-options-icon">
                                        <i class="el-icon-edit" @click="dialogItemDeleted(scope)"></i>
                                    </span>
                                    </template>
                                </el-table-column>
                                <el-table-column width="100" :label="$t('platform.user.operation')" align="right" fixed="right">
                                    <template slot-scope="scope">
                                        <span class="operation-options-icon">
                                            <i class="el-icon-edit-outline" @click="dialogItemUpdate(scope)"></i>
                                        </span>
                                        <span class="operation-options-icon">
                                           <i class="el-icon-delete" @click="dialogItemDestroy(scope)"></i>
                                        </span>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </component-page-content-resize>
                        <div class="text-right p-4">
                            <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                                           :current-page="filterOption.page"
                                           :page-size.sync="filterOption.page_size"
                                           :total="dataMeta.total"
                                           @size-change="filterChange"
                                           @current-change="filterPageChange">
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog :title="$t('platform.user.dialog.deleted')" center width="320px" :visible.sync="dialog.visible.deleted">
            <div class="text-center" v-html="$t('confirm.user', { status: !itemCache.deleted ? $t('action.off') : $t('action.on') })"></div>
            <div slot="footer">
                <el-button type="primary" @click="deleted">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.deleted = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <el-dialog :title="$t('platform.user.dialog.destroy')" center width="340px" :visible.sync="dialog.visible.destroy">
            <div class="text-center" v-html="$t('confirm.user', { status: $t('action.delete') })"></div>
            <div class="text-center text-danger-custom">{{ $t('confirm.delete') }}</div>
            <div slot="footer">
                <el-button type="danger" @click="destroy">{{ $t('action.confirm') }}</el-button>
                <el-button @click="dialog.visible.destroy = false">{{ $t('action.cancel') }}</el-button>
            </div>
        </el-dialog>
        <item-create :visible.sync="dialog.visible.create" :org="tree" @create="filterChange"></item-create>
        <item-update :visible.sync="dialog.visible.update" :data="itemCache" @update="filterChange"></item-update>
    </div>
</template>

<script>
import ItemCreate from './ItemCreate.vue'
import ItemUpdate from './ItemUpdate.vue'

export default {
    components: {
        ItemCreate,
        ItemUpdate,
    },
    data() {
        return {
            tree: {},
            loading: false,
            filterOption: {
                org_id: null,
                deleted: '',
                keyword: '',
                page: 1,
                page_size: 30,
                order: 'desc',
                order_key: 'descending',
                order_column: 'updated',
            },
            dataMeta: {
                total: 0,
            },
            dataList: [],
            itemCache: {},
            dialog: {
                visible: {
                    create: false,
                    update: false,
                    deleted: false,
                    destroy: false,
                }
            }
        }
    },
    methods: {
        treeSelectNode(data) {
            // 缓存数据
            this.tree = data;
            // 平台筛选
            this.filterOption.org_id = data.id;
            // 初始化数据
            this.filterChange();
        },
        clearFilterOption() {
            this.loading = true;
            this.filterOption.page = 1;
        },
        filterChange() {
            this.clearFilterOption();
            this.getDataList();
        },
        filterPageChange(page) {
            this.loading = true;
            this.filterOption.page = page;
            this.getDataList();
        },
        filterOrderChange(scope) {
            this.clearFilterOption();
            this.filterOption.order = scope.order == 'ascending' ? 'asc' : 'desc';
            this.filterOption.order_key = scope.order;
            this.filterOption.order_column = scope.prop;
            this.getDataList();
        },
        getDataList() {
            axios.get('/platform/user/getList', {
                params: this.filterOption
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.dataMeta = response.data.resp_data.meta;
                    this.dataList = response.data.resp_data.data;
                } else {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                }
                this.loading = false;
            })
        },
        // 删除数据/恢复数据
        deleted() {
            // 关闭提示框
            this.dialog.visible.deleted = false;
            // loading 状态 start
            let loading = this.$loading();
            // 删除/恢复
            let action = !this.itemCache.deleted ? 'destroy' : 'restore';
            // 保存数据
            axios.post(`/platform/user/${action}`, {
                id: this.itemCache.id
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {
                            status: !this.itemCache.deleted ? this.$t('action.off') : this.$t('action.on')
                        }),
                        showClose: true
                    });
                    // 刷新列表
                    this.filterChange();
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {
                            status: !this.itemCache.deleted ? this.$t('action.off') : this.$t('action.on')
                        }) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        // 彻底删除数据
        destroy() {
            // 关闭提示框
            this.dialog.visible.destroy = false;
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/user/forceDestroy', {
                id: this.itemCache.id
            }).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.delete')}),
                        showClose: true
                    });
                    // 刷新列表
                    this.filterChange();
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.delete')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        // 显示模态框（新增）
        dialogItemCreate() {
            this.dialog.visible.create = true;
        },
        // 显示模态框（编辑）
        dialogItemUpdate(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.update = true;
        },
        // 显示模态框（删除/恢复）
        dialogItemDeleted(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.deleted = true;
        },
        // 显示模态框（彻底删除）
        dialogItemDestroy(scope) {
            // 缓存数据
            this.itemCache = scope.row;
            // 显示模态框
            this.dialog.visible.destroy = true;
        },
    }
}
</script>

<style scoped>

</style>
